//
//  WeatherVController.m
//  Travel assistant
//
//  Created by lanou3g on 16/2/29.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "WeatherVController.h"
#import "JourneyTVController.h"

@interface WeatherVController ()

@end

@implementation WeatherVController

- (instancetype)init{
    if ([super init]) {
        //self.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    _blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    _effectview = [[UIVisualEffectView alloc] initWithEffect:_blur];
    _effectview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self.view addSubview: _effectview];
    [self setMenu];
    // 定时器控制每个按钮弹出的时间
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(popupBtn) userInfo:nil repeats:YES];
    
    self.diyLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 25, 100, 30)];
    self.diyLable.text = @"自定义天气";
    [self.effectview addSubview:self.diyLable];
    
    self.cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancleButton setBackgroundImage:[UIImage imageNamed:@"Go_back_32px_1186174_easyicon.net"] forState:UIControlStateNormal];
    self.cancleButton.frame = CGRectMake(Screen_W - 40, 25, 30, 30);
    [self.effectview addSubview:self.cancleButton];
    [self.cancleButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    

    
}
/**视图即将呈现*/
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    
    
    
}
- (void)cancelButtonAction:(UIButton *)sender{
    UINavigationController *journeyNaVC = [[UINavigationController alloc] initWithRootViewController:[[JourneyTVController alloc] init]];
    
    [self presentViewController:journeyNaVC animated:YES completion:^{
        NSLog(@"点击返回按钮。。。。");
    }];
    
    
    
    
    
    
    
//    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[[JourneyTVController alloc] init]] animated:YES completion:nil];

    //journeyNaVC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;


    //self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(returnUpVC) userInfo:nil repeats:YES];
}

- (NSArray *)weatherImVs{
    if (!_weatherImVs) {
        _weatherImVs = [NSArray arrayWithArray:[ShareDataHelper shareDataHelper].weatherArray];
    }
    return _weatherImVs;
}
- (NSMutableArray *)itemButtons
{
    if (_itemButtons == nil) {
        _itemButtons = [NSMutableArray array];
    }
    return _itemButtons;
}
- (void)popupBtn{
    
    if (_upIndex == self.itemButtons.count) {
        
        [self.timer invalidate];
        
        _upIndex = 0;
        return;
    }
    
    UIButton *btn = self.itemButtons[_upIndex];
    
    [self setUpOneBtnAnim:btn];
    
    _upIndex++;
}
//设置按钮从第一个开始向上滑动显示
- (void)setUpOneBtnAnim:(UIButton *)btn
{
    
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        btn.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished){
        
        //获取当前显示的菜单控件的索引
        _downIndex = self.itemButtons.count - 1;
    }];
    
}
//按九宫格计算方式排列按钮
- (void)setMenu{
    
    int cols = 3;
    int col = 0;
    int row = 0;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat wh = 90;
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width - cols * wh) / (cols + 1);
    CGFloat oriY = [UIScreen mainScreen].bounds.size.height / 2 - wh;
    
    for (int i = 0; i < 6; i++) {
        UIButton *weatherBut = [UIButton buttonWithType:UIButtonTypeCustom];
        WeatherModel *model = self.weatherImVs[i];
        UIImage *img = [UIImage imageNamed:model.image];
        [weatherBut setImage:img forState:UIControlStateNormal];
    
        col = i % cols;
        row = i / cols;
        
        x = margin + col * (margin + wh);
        y = row * (margin + wh) + oriY;
        
        weatherBut.frame = CGRectMake(x, y, wh, wh);
        weatherBut.layer.cornerRadius = wh / 2;
        weatherBut.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
        
        weatherBut.tag = 1000 + i;
        
        
        [weatherBut addTarget:self action:@selector(weatherButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.itemButtons addObject:weatherBut];
        [_effectview.contentView addSubview:weatherBut];
    }
    
}
//点击weather按钮进行放大动画效果直到消失
- (void)weatherButtonAction:(UIButton *)weatherBut{

    //NSLog(@"~~~~~~~%ld",weatherBut.tag - 1000);
    UINavigationController *journeyNaVC = [[UINavigationController alloc] initWithRootViewController:[[JourneyTVController alloc] init]];
    //journeyNaVC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
    [UIView animateWithDuration:0.5 animations:^{
        weatherBut.transform = CGAffineTransformMakeScale(10, 10);
        weatherBut.alpha = 0;
        [self presentViewController:journeyNaVC animated:YES completion:^{
            weatherBut.transform = CGAffineTransformMakeScale(1, 1);
            weatherBut.alpha = 1;
        }];
    }];
    if (self.weatherBlock) {
        self.weatherBlock(weatherBut.tag - 10000);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
